// _GraphPropertiesMinMax.cpp : implementation file
//

#include "stdafx.h"
#include "FlexGraph.h"
#include "_GraphPropertiesMinMax.h"
#include "FlexGraphInterface.h"


// _GraphPropertiesMinMax dialog

IMPLEMENT_DYNAMIC(_GraphPropertiesMinMax, CDialogEx)

_GraphPropertiesMinMax::_GraphPropertiesMinMax(CWnd* pParent /*=NULL*/)
    : CDialogEx(_GraphPropertiesMinMax::IDD, pParent)
{
    NoUpdate = false;
}

_GraphPropertiesMinMax::~_GraphPropertiesMinMax()
{
}

void _GraphPropertiesMinMax::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT_MAX_X, m_EditMaxX);
    DDX_Control(pDX, IDC_EDIT_MAX_Y, m_EditMaxY);
    DDX_Control(pDX, IDC_EDIT_MIN_X, m_EditMinX);
    DDX_Control(pDX, IDC_EDIT_MIN_Y, m_EditMinY);
    DDX_Control(pDX, IDC_EDIT_TITLE, m_EditTitle);
    DDX_Control(pDX, IDC_EDIT_XCAPTION, m_EditXCaption);
    DDX_Control(pDX, IDC_EDIT_YCAPTION, m_EditYCaption);
    DDX_Control(pDX, IDC_COMBO_BORDER_SIZE, m_ComboBorderSize);
    DDX_Control(pDX, IDC_ColorCtrlBorder, m_ColorCtrlBorderColor);
    DDX_Control(pDX, IDC_CHECK_GRID, m_CheckGrid);
    DDX_Control(pDX, IDC_CHECK_TRACKING, m_CheckTracking);
    DDX_Control(pDX, IDC_ColorCtrlBackground, m_ColorControlBackground);
    DDX_Control(pDX, IDC_COLOR_CONTROL_AXIS, m_ColorControlAxis);
}


BEGIN_MESSAGE_MAP(_GraphPropertiesMinMax, CDialogEx)
    ON_WM_SHOWWINDOW()
    ON_WM_CTLCOLOR()
    ON_BN_CLICKED(IDC_BUTTON_RESET_TO_DEFAULT, &_GraphPropertiesMinMax::OnBnClickedButtonResetToDefault)
    ON_CBN_SELCHANGE(IDC_COMBO_BORDER_SIZE, &_GraphPropertiesMinMax::OnCbnSelchangeComboBorderSize)
    ON_BN_CLICKED(IDC_ColorCtrlBorder, &_GraphPropertiesMinMax::OnClickedColorctrlborder)
    ON_BN_CLICKED(IDC_CHECK_GRID, &_GraphPropertiesMinMax::OnBnClickedCheckGrid)
    ON_BN_CLICKED(IDC_CHECK_TRACKING, &_GraphPropertiesMinMax::OnBnClickedCheckTracking)
    ON_EN_CHANGE(IDC_EDIT_TITLE, &_GraphPropertiesMinMax::OnChangeEditTitle)
    ON_EN_CHANGE(IDC_EDIT_XCAPTION, &_GraphPropertiesMinMax::OnChangeEditXcaption)
    ON_EN_CHANGE(IDC_EDIT_YCAPTION, &_GraphPropertiesMinMax::OnChangeEditYcaption)
    ON_EN_CHANGE(IDC_EDIT_MIN_Y, &_GraphPropertiesMinMax::OnEnChangeEditMinY)
    ON_EN_CHANGE(IDC_EDIT_MIN_X, &_GraphPropertiesMinMax::OnEnChangeEditMinX)
    ON_EN_CHANGE(IDC_EDIT_MAX_X, &_GraphPropertiesMinMax::OnEnChangeEditMaxX)
    ON_EN_CHANGE(IDC_EDIT_MAX_Y, &_GraphPropertiesMinMax::OnEnChangeEditMaxY)
    ON_BN_CLICKED(IDC_ColorCtrlBackground, &_GraphPropertiesMinMax::OnBnClickedColorctrlbackground)
    ON_BN_CLICKED(IDC_COLOR_CONTROL_AXIS, &_GraphPropertiesMinMax::OnBnClickedColorControlAxis)
END_MESSAGE_MAP()


// _GraphPropertiesMinMax message handlers

afx_msg HBRUSH _GraphPropertiesMinMax::OnCtlColor(
    CDC* pDC,
    CWnd* pWnd,
    UINT nCtlColor
)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    pDC->SetBkMode(TRANSPARENT);
    return m_hbr;
}

void _GraphPropertiesMinMax::OnBnClickedButtonApplyMinMax()
{
    CString Temp;
    double X_Min, X_Max, Y_Min, Y_Max;
    m_EditMaxX.GetWindowText(Temp);
    Temp.Replace(',', '.');
    swscanf_s(Temp, _T("%lf"), &X_Max);
    m_EditMinX.GetWindowText(Temp);
    Temp.Replace(',', '.');
    swscanf_s(Temp, _T("%lf"), &X_Min);

    if(abs(X_Min) > FLT_MAX || abs(X_Max) > FLT_MAX || X_Min >= X_Max)
    {
        return;
    }

    m_EditMaxY.GetWindowText(Temp);
    Temp.Replace(',', '.');
    swscanf_s(Temp, _T("%lf"), &Y_Max);
    m_EditMinY.GetWindowText(Temp);
    Temp.Replace(',', '.');
    swscanf_s(Temp, _T("%lf"), &Y_Min);

    if(_isnan(Y_Min) || _isnan(Y_Max) || _isnan(X_Min) || _isnan(X_Max))
    {
        return;
    }

    if(abs(Y_Min) > FLT_MAX || abs(Y_Max) > FLT_MAX || Y_Min >= Y_Max)
    {
        return;
    }

    FG_setMinMax(m_GraphControlHandle, X_Min, X_Max, Y_Min, Y_Max);
    FG_requestRedraw(m_GraphControlHandle);
    //if (!NoUpdate) updateMinMaxEditBoxes();
}


void _GraphPropertiesMinMax::OnShowWindow(BOOL bShow, UINT nStatus)
{
    CDialogEx::OnShowWindow(bShow, nStatus);

    if(bShow)
    {
        EnableThemeDialogTexture(this->GetSafeHwnd(), ETDT_ENABLETAB);
        CString Temp;
        Temp.Format(_T("%d"), m_pGraphControl->getBorderSize());
        m_ComboBorderSize.SelectString(0, Temp);
        m_ColorCtrlBorderColor.EnableOtherButton(_T("Other"), FALSE, TRUE);
        m_ColorControlAxis.EnableOtherButton(_T("Other"), FALSE, TRUE);
        m_ColorControlBackground.EnableOtherButton(_T("Other"), FALSE, TRUE);
        m_ColorControlAxis.SetColor(m_pGraphControl->getAxisColor());
        m_ColorControlBackground.SetColor(m_pGraphControl->m_BackgroundColor);
        m_CheckGrid.SetCheck(m_pGraphControl->m_DrawGrid);
        m_CheckTracking.SetCheck(m_pGraphControl->getMouseTrackingEnabled());
        m_EditTitle.SetWindowText(m_pGraphControl->getTitle());
        m_EditXCaption.SetWindowText(m_pGraphControl->getXCaption());
        m_EditYCaption.SetWindowText(m_pGraphControl->getYCaption());
        updateMinMaxEditBoxes();
    }
}


void _GraphPropertiesMinMax::OnBnClickedButtonResetToDefault()
{
    FG_setDefaultZoom(m_GraphControlHandle);
    updateMinMaxEditBoxes();
}

void _GraphPropertiesMinMax::updateMinMaxEditBoxes()
{
    double x0, x1, y0, y1;
    CString Temp;
    NoUpdate = true;
    FG_getMinMax(m_GraphControlHandle, &x0, &x1, &y0, &y1);
    Temp.Format(_T("%.5E"), x0);
    m_EditMinX.SetWindowText(Temp);
    //m_EditMinX.SetSel(0,-1); // select all text and move cursor at the end
    //m_EditMinX.SetSel(-1); //  remove selection
    //
    Temp.Format(_T("%.5E"), x1);
    m_EditMaxX.SetWindowText(Temp);
    //m_EditMaxX.SetSel(0,-1); // select all text and move cursor at the end
    //m_EditMaxX.SetSel(-1); //  remove selection
    //
    Temp.Format(_T("%.5E"), y0);
    m_EditMinY.SetWindowText(Temp);
    //m_EditMinY.SetSel(0,-1); // select all text and move cursor at the end
    //m_EditMinY.SetSel(-1); //  remove selection
    //
    Temp.Format(_T("%.5E"), y1);
    m_EditMaxY.SetWindowText(Temp);
    //m_EditMaxY.SetSel(0,-1); // select all text and move cursor at the end
    //m_EditMaxY.SetSel(-1); //  remove selection
    NoUpdate = false;
}

void _GraphPropertiesMinMax::OnOK()
{
    OnBnClickedButtonApplyMinMax();
}


void _GraphPropertiesMinMax::OnCancel()
{
}


void _GraphPropertiesMinMax::OnChangeEditTitle()
{
    CString Temp;
    m_EditTitle.GetWindowText(Temp);
    m_pGraphControl->setTitle(Temp);
    FG_requestRedraw(m_GraphControlHandle);
}


void _GraphPropertiesMinMax::OnChangeEditXcaption()
{
    CString Temp;
    m_EditXCaption.GetWindowText(Temp);
    m_pGraphControl->setXCaption(Temp);
    FG_requestRedraw(m_GraphControlHandle);
}


void _GraphPropertiesMinMax::OnChangeEditYcaption()
{
    CString Temp;
    m_EditYCaption.GetWindowText(Temp);
    m_pGraphControl->setYCaption(Temp);
    FG_requestRedraw(m_GraphControlHandle);
}

void _GraphPropertiesMinMax::OnCbnSelchangeComboBorderSize()
{
    int Selected = m_ComboBorderSize.GetCurSel();
    CString Temp;
    m_ComboBorderSize.GetLBText(Selected, Temp);
    _u32 TempSize;
    swscanf_s(Temp, _T("%d"), &TempSize);
    m_pGraphControl->setBorderSize(TempSize);
    FG_requestRedraw(m_GraphControlHandle);
}


void _GraphPropertiesMinMax::OnClickedColorctrlborder()
{
    m_pGraphControl->setBorderColor(m_ColorCtrlBorderColor.GetColor());
    FG_requestRedraw(m_GraphControlHandle);
}


void _GraphPropertiesMinMax::OnBnClickedCheckGrid()
{
    m_pGraphControl->m_DrawGrid = m_CheckGrid.GetCheck() != 0;
    FG_requestRedraw(m_GraphControlHandle);
}


void _GraphPropertiesMinMax::OnBnClickedCheckTracking()
{
    m_pGraphControl->setMouseTrackingEnabled(m_CheckTracking.GetCheck() != 0);
    FG_requestRedraw(m_GraphControlHandle);
}


void _GraphPropertiesMinMax::OnEnChangeEditMinY()
{
    NoUpdate = true;
    OnBnClickedButtonApplyMinMax();
    NoUpdate = false;
}


void _GraphPropertiesMinMax::OnEnChangeEditMinX()
{
    NoUpdate = true;
    OnBnClickedButtonApplyMinMax();
    NoUpdate = false;
}


void _GraphPropertiesMinMax::OnEnChangeEditMaxX()
{
    NoUpdate = true;
    OnBnClickedButtonApplyMinMax();
    NoUpdate = false;
}


void _GraphPropertiesMinMax::OnEnChangeEditMaxY()
{
    NoUpdate = true;
    OnBnClickedButtonApplyMinMax();
    NoUpdate = false;
}


void _GraphPropertiesMinMax::OnBnClickedColorctrlbackground()
{
    m_pGraphControl->setBackgroundColor(m_ColorControlBackground.GetColor());
    FG_requestRedraw(m_GraphControlHandle);
}


void _GraphPropertiesMinMax::OnBnClickedColorControlAxis()
{
    m_pGraphControl->setAxisColor(m_ColorControlAxis.GetColor());
    FG_requestRedraw(m_GraphControlHandle);
}
